package com.chixing.service.impl;

import com.chixing.mapper.CustomerMapper;
import com.chixing.pojo.Customer;
import com.chixing.pojo.CustomerExample;
import com.chixing.service.LoginAndRegisterService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LoginAndRegisterServiceImpl implements LoginAndRegisterService {
    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public Customer login(Long cusTelno, String cusPassword) {
        CustomerExample customerExample = new CustomerExample();
        customerExample.createCriteria().andCusTelnoEqualTo(cusTelno).andCusPasswordEqualTo(cusPassword);
        List<Customer> customers = customerMapper.selectByExample(customerExample);
        for (Customer customer:customers){
            if (cusTelno.longValue()==customer.getCusTelno().longValue() && cusPassword.equals(customer.getCusPassword())){
                return customer;
            }
        }
        return null;
    }

    @Override
    public ServerResponse register(Customer customer){
        CustomerExample customerExample = new CustomerExample();
        customerExample.createCriteria().andCusTelnoEqualTo(customer.getCusTelno());
        CustomerExample customerExample1 = new CustomerExample();
        customerExample1.createCriteria().andCusNicknameEqualTo(customer.getCusNickname());
        List<Customer> customers = customerMapper.selectByExample(customerExample);
        for (Customer customer1:customers){
            if (customer.getCusTelno().intValue() == customer1.getCusTelno().intValue()){
                return ServerResponse.saveFail(null);
            }
        }
        List<Customer> customers1 = customerMapper.selectByExample(customerExample1);
        for (Customer customer1:customers1){
            if (customer.getCusNickname().equals(customer1.getCusNickname())){
                return ServerResponse.saveFail(null);
            }
        }
        int rows = customerMapper.insert(customer);
        if (rows > 0)
            return ServerResponse.saveSuccess(rows);
        return ServerResponse.saveFail(null);
    }
}
